﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Partial Class contact
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 10
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
       LoadContact()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadContact()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadContact()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
       LoadContact()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadContact()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
       LoadContact()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
       LoadContact()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadContact()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
       LoadContact()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadContact()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
     LoadContact()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadContact()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
       LoadContact()
    End Sub

#End Region

    Private Sub LoadContact()

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()
            If txtSearch.Text = "" Then
                objDB.dbVariable.SQLString = "SELECT ContactId,Name, CASE WHEN ProspectiveType=1 THEN 'Purchaser' " & _
                "WHEN ProspectiveType=2 THEN 'Tenant' WHEN ProspectiveType=3 THEN 'Vendor' WHEN ProspectiveType=4 " & _
                "THEN 'Landlord' WHEN ProspectiveType=5 THEN 'Developer' WHEN ProspectiveType=6 THEN 'Investor' " & _
                "WHEN ProspectiveType=7 THEN 'Banker' ELSE 'Others' END AS ProspectiveType, Phone, Mobile, Email FROM AW_Contact " & _
                "WHERE StatusDelete=0" & lblHidSortType.Text
                objDB.FillData(TempDT, "AW_Contact", Database.SQLCommandType.NormalString)
            Else
                objDB.dbVariable.SQLString = "SELECT ContactId,Name, CASE WHEN ProspectiveType=1 THEN 'Purchaser' " & _
                "WHEN ProspectiveType=2 THEN 'Tenant' WHEN ProspectiveType=3 THEN 'Vendor' WHEN ProspectiveType=4 " & _
                "THEN 'Landlord' WHEN ProspectiveType=5 THEN 'Developer' WHEN ProspectiveType=6 THEN 'Investor' " & _
                "WHEN ProspectiveType=7 THEN 'Banker' ELSE 'Others' END AS ProspectiveType, Phone, Mobile, Email " & _
                "FROM AW_Contact WHERE StatusDelete=0 AND (Name LIKE @Name OR Mobile LIKE @Mobile OR Phone LIKE @Phone)" & lblHidSortType.Text
                objDB.AddParameter("Name", Data.SqlDbType.NVarChar, "%" & txtSearch.Text & "%")
                objDB.AddParameter("Mobile", Data.SqlDbType.NVarChar, "%" & txtSearch.Text & "%")
                objDB.AddParameter("Phone", Data.SqlDbType.NVarChar, "%" & txtSearch.Text & "%")
                objDB.FillData(TempDT, "AW_Contact", Database.SQLCommandType.NormalString)
            End If
            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = TempDT.DefaultView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repContact.DataSource = objPaged
        repContact.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Protected Sub imgNewContact_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgNewContact.Click
        Response.Redirect("contactentry.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        ElseIf Session.Item("StaffType").ToString = "2" Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            '0-Back Page
            '1-Search
            '3-Page Size
            '4-Current Page
            If arr(0).ToString.Contains("contact.aspx") Then
                txtSearch.Text = arr(1).ToString
                PageSize = CInt(arr(2).ToString)
                CurrentPage = CInt(arr(3).ToString)
                Session.Remove("Back")
                LoadContact()
            Else
                Session.Remove("Back")
            End If
        End If

        If Not Page.IsPostBack Then
            LoadContact()
            If Not Session.Item("entry") Is Nothing Then
                lblMsgSuccessful.Text = "<strong>Information:</strong>Contact <strong>" & Session.Item("entry").ToString & "</strong> was created"
                divSuccessful.Visible = True
                Session.Remove("entry")
            End If

            If Not Session.Item("entryU") Is Nothing Then
                lblMsgSuccessful.Text = "<strong>Information:</strong>Contact <strong>" & Session.Item("entryU").ToString & "</strong> was updated"
                divSuccessful.Visible = True
                Session.Remove("entryU")
            End If
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadContact()
    End Sub

    Protected Sub repContact_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repContact.ItemCommand

        If e.CommandName.ToUpper = "EDITITEM" Then
            'Edited by minginn - To allow user to go back to the search page with same search selection
            'Back Page
            'Search           
            Dim strBack As String
            strBack = "contact.aspx" & "ڡ" & txtSearch.Text.Trim & "ڡ" & PageSize & "ڡ" & CurrentPage
            Session.Add("Back", strBack)
            Dim contactUID As String = CType(repContact.Items(e.CommandArgument).FindControl("lblRepContactUID"), Label).Text
            Response.Redirect("contactentry.aspx?id=" & SymmetricEncryptToHex(contactUID, "augment"))
        Else
            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE AW_Contact SET StatusDelete=1 WHERE ContactId=@ContactId"
                objDB.AddParameter("ContactId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repContact.Items(e.CommandArgument).FindControl("lblRepContactUID"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
                lblMsgSuccessful.Text = "Contact Deleted"
                lblMsgSuccessful.Visible = True
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
            LoadContact()
        End If
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkName.Click
        If lnkName.Text = "Contact Name▼" Then
            lnkName.Text = "Contact Name▲"
        Else
            lnkName.Text = "Contact Name▼"
        End If

        lblHidSortType.Text = " ORDER BY Name " & SortType(lnkName.Text) & ", ProspectiveType " & SortType(lnkProspectiveType.Text) & _
                            ", Phone " & SortType(lnkPhone.Text) & ", Mobile " & SortType(lnkMobile.Text) & _
                            ", Email " & SortType(lnkEmail.Text)
        LoadContact()
    End Sub

    Protected Sub lnkProspectiveType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkProspectiveType.Click
        If lnkProspectiveType.Text = "Prospective Type▼" Then
            lnkProspectiveType.Text = "Prospective Type▲"
        Else
            lnkProspectiveType.Text = "Prospective Type▼"
        End If

        lblHidSortType.Text = " ORDER BY ProspectiveType " & SortType(lnkProspectiveType.Text) & ", Name " & SortType(lnkName.Text) & _
                            ", Phone " & SortType(lnkPhone.Text) & ", Mobile " & SortType(lnkMobile.Text) & _
                            ", Email " & SortType(lnkEmail.Text)
        LoadContact()
    End Sub

    Protected Sub lnkPhone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPhone.Click
        If lnkPhone.Text = "Phone▼" Then
            lnkPhone.Text = "Phone▲"
        Else
            lnkPhone.Text = "Phone▼"
        End If

        lblHidSortType.Text = " ORDER BY Phone " & SortType(lnkPhone.Text) & ", Name " & SortType(lnkName.Text) & _
                            ", StaffType " & SortType(lnkProspectiveType.Text) & ", Mobile " & SortType(lnkMobile.Text) & _
                            ", Email " & SortType(lnkEmail.Text)
        LoadContact()
    End Sub

    Protected Sub lnkMobile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkMobile.Click
        If lnkMobile.Text = "Mobile▼" Then
            lnkMobile.Text = "Mobile▲"
        Else
            lnkMobile.Text = "Mobile▼"
        End If

        lblHidSortType.Text = " ORDER BY Mobile " & SortType(lnkMobile.Text) & ", Name " & SortType(lnkName.Text) & _
                            ", StaffType " & SortType(lnkProspectiveType.Text) & ", Phone " & SortType(lnkPhone.Text) & _
                            ", Email " & SortType(lnkEmail.Text)
        LoadContact()
    End Sub

    Protected Sub lnkEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkEmail.Click
        If lnkEmail.Text = "Email▼" Then
            lnkEmail.Text = "Email▲"
        Else
            lnkEmail.Text = "Email▼"
        End If

        lblHidSortType.Text = " ORDER BY Email " & SortType(lnkEmail.Text) & ", Name " & SortType(lnkName.Text) & _
                            ", StaffType " & SortType(lnkProspectiveType.Text) & ", Phone " & SortType(lnkPhone.Text) & _
                            ", Mobile " & SortType(lnkMobile.Text)
        LoadContact()
    End Sub

    Protected Sub repContact_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repContact.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            CType(e.Item.FindControl("cbeDelete"), Object).confirmText = "Are you sure you want to delete " & CType(e.Item.FindControl("lblRepContactName"), Label).Text
        End If
    End Sub
End Class
